//
//  JCPictureLayout.swift
//  weibo07777
//
//  Created by mac on 15/12/23.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

protocol JCPictureLayoutDelegate : NSObjectProtocol{
    
    func pictureLayout(layout: JCPictureLayout)->(CGFloat)
    
}

class JCPictureLayout: UICollectionViewLayout {

    weak var delegate :JCPictureLayoutDelegate?
    
    /** 每一列之间的间距 */
    let JCDefaultColumnMargin = CGFloat(10)
    /** 每一行之间的间距 */
    let JCDefaultRowMargin = CGFloat(10)
    
    let JCDefaultWidth = CGFloat(90)
    let JCDefaultHeight = CGFloat(90)
    /** 边缘间距 */
    let JCDefaultEdgeInsets = UIEdgeInsetsZero
    /** 存放所有cell的布局属性 */
    var attrsArray : [UICollectionViewLayoutAttributes]?
    /** 图片数量*/
    var pictureCount = 0

    // MARK : - 初始化
    override func prepareLayout() {
        super.prepareLayout()
        // 清除之前所有的布局属性
        attrsArray?.removeAll()
        // 获得总图片
        pictureCount = collectionView!.numberOfItemsInSection(0)
        
//        print("pictureCount=\(pictureCount)")
        
        var temp = [UICollectionViewLayoutAttributes]()
        
        for var index = 0;index < pictureCount;index++ {
        
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            
            let attrs = layoutAttributesForItemAtIndexPath(indexPath)
            
            temp.append(attrs!)
        }
        attrsArray = temp
    }

    // MARK :3ge method
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return attrsArray
    }
    
    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        
        var attrLayout = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        
        let columnCount = delegate?.pictureLayout(self) ?? 0
        
        if columnCount == 0 {
            return attrLayout
        }
        let collectionViewW = collectionView?.frame.width
        
//        let W = (collectionViewW! - JCDefaultEdgeInsets.left - JCDefaultEdgeInsets.right - (columnCount - 1) * JCDefaultColumnMargin) / columnCount
        var W = JCDefaultWidth
        
        var H = JCDefaultHeight
        
        let row = indexPath.row / Int(columnCount)
        let col = indexPath.row % Int(columnCount)
        
        var x = JCDefaultEdgeInsets.left + CGFloat(col) * (JCDefaultRowMargin + W)
        
        var y = JCDefaultEdgeInsets.top + CGFloat(row) * (JCDefaultColumnMargin + H)
        
        if pictureCount == 1 {
        
            W = self.collectionView!.frame.width
            H = self.collectionView!.frame.height
            x = 0
            y = 0
            attrLayout.frame = CGRectMake(x, y, W, H)
            return attrLayout
        }
    
        attrLayout.frame = CGRectMake(x, y, W, H)
        return attrLayout
        
    }
    
    override func collectionViewContentSize() -> CGSize {
        
        if collectionView?.frame.size.height == 0 {
            return CGSizeZero
        }
        return collectionView!.frame.size
    }
    
}
